/*
 *-------------------------------------------------------------
 *    copyrights by
 *    	Bartosz "Dust" Kowalski (dustization.info)
 *    	Maciej Krzysica 
 *-------------------------------------------------------------
 *    Plik: 	PCMDemodulation.java
 */

package core;

class PCMDemodulation implements PCMOperation {

	private int interval = 0;
	
	private boolean recalculate = true;
	
	private double currentValue = 0;

	@Override
	public PCMOperationData operate(DataPackage dataPackage, PCMSettings settings) {
		
		if(recalculate)
		{
			interval = (int) (dataPackage.sampleRate / (settings.getSampleRate()));
			recalculate = false;
			
			double step = 2.0 / Math.pow(2.0, settings.getResolution());
			currentValue = (step * dataPackage.data[0]) - 1;
		}
		dataPackage.data[0] = currentValue;
		
		if ( --interval == 0 )
		{
			recalculate = true;
			return new PCMOperationData(dataPackage, true);
		}
		
		return new PCMOperationData(dataPackage, false);

	}
	
}
